/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phan.connection;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import phan.connection.InscriptionBean;
import java.sql.*;


/**
 *
 * @author BAILLEURS SERVICES
 */


public class DBHandler extends HttpServlet {

    public void doPost (HttpServletRequest request, HttpServletResponse response) {

   /*     HttpSession session = request.getSession(true);
		// Recupere l'age de l'utilisateur
		String userName= (String)session.getAttribute("login");
        String password= (String)session.getAttribute("motDePasse");
        String fname= (String)session.getAttribute("nom");
        String lname= (String)session.getAttribute("prenom");
        String mail=(String)session.getAttribute("email");
        String adr= (String)session.getAttribute("adress");
        String city=(String)session.getAttribute("ville");
        String country=(String)session.getAttribute("pays");
        String zip=(String)session.getAttribute("postal");
        String phone=(String)session.getAttribute("tel");
        String des=(String)session.getAttribute("desc");
        int role=2;int acc=0;  */

     try {
      InscriptionBean f = (InscriptionBean) request.getAttribute("formHandler");
      String user = f.getLogin();
      System.out.print(user);
      boolean userExists = false;
      //obtain a db connection and perform a db query
      //ensuring that the username does not exist
      //set userExists=true if user is found in db
      //for a simple test, you can disallow the registration
      //of the username "rogerm" as:
      //if (f.getUserName().equals("rogerm")) userExists=true;
      if (userExists) {
        f.setErrors("login","Duplicate User: Try a different username");
          getServletConfig().getServletContext().
          getRequestDispatcher("retry.jsp").
          forward(request, response);
      } else {
        //retrieve the bean properties and store them
        // into the database.

       //   MySQLDBConnector conx=new MySQLDBConnector();
       //   Connection con=conx.connect();
       //   Statement st=con.createStatement();
      //    String query="insert into usertable(UserNom,UserPrenom,UserEmail,UserAdresse,UserVille,UserCode,UserPays,UserTelephone,UserRoleID,Login,Password,activeAcc) values('"+fname+"','"+lname+"','"+mail+"','"+adr+"','"+city+"','"+zip+"','"+country+"','"+phone+"','"+role+"','"+userName+"','"+password+"','"+acc+"')";
      //    st.executeUpdate(query);

          getServletConfig().getServletContext().
          getRequestDispatcher("CheckInscription.jsp").
          forward(request, response);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }


}
